package com.jfinal.enjoy

import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.openapi.editor.Editor

/**
 * @author 杜文康
 * @date  2025/8/23 22:06
 */
class TemplateInsertHandler(
    private val template: String,
    private val caretOffset: Int
) : InsertHandler<LookupElement> {
    override fun handleInsert(context: InsertionContext, item: LookupElement) {
        val editor: Editor = context.editor
        val document = editor.document

        val startOffset = context.startOffset
        val tailOffset = context.tailOffset

        // 插入模板
        document.replaceString(startOffset, tailOffset, template)

        // 移动光标
        editor.caretModel.moveToOffset(startOffset + caretOffset)
    }
}